package controller
{
    import data.Servant;

    import flash.events.TimerEvent;

    import data.Servant;
    import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	/**
	 * ...
	 * @author yaempechenki@gmail.com
	 */
	public class NowTrack 
	{
		private const SONG_REQUEST_TIMEOUT:int = 5000;
		private var timer:Timer;
		private var serv:Servant;
		private var _enable:Boolean;
		
		private var textField:TextField;
		public function NowTrack(tf:TextField) 
		{
			textField = tf;
			timer = new Timer(SONG_REQUEST_TIMEOUT);
			timer.addEventListener(TimerEvent.TIMER, onTimer);
			timer.start();
			enable = false;
			
		}
		
		public function set enable(value:Boolean):void {
			_enable = value;
			if (!_enable) {
				textField.text = "Zong-radio.ru";
			}
		}
		
		private function onTimer(event:TimerEvent):void 
		{
			if (!_enable) {
				return;
			}
			if (serv) {
				serv.destroy();
			}
			serv = new Servant();
			serv.onComplete = getSongNameHandler;
			serv.getSong(Config.SONG_NAME_URL);
		}
		
		public function getSongNameHandler(songName:String):void {
			if (songName.length >= 51) {
				songName = songName.substring (0, 48) + "...";
			}
			textField.text = songName;
		}
	}
}